package com.ctshk.rpc.order.tour.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ctshk.common.dto.Result;
import com.ctshk.common.enums.SystemError;
import com.ctshk.common.exception.BusinessException;
import com.ctshk.common.utils.CharUtil;
import com.ctshk.common.utils.EntityUtil;
import com.ctshk.rpc.order.tour.constant.Constants;
import com.ctshk.rpc.order.tour.dto.SeriesOrderTouristQuitNoticeDetailDTO;
import com.ctshk.rpc.order.tour.entity.SeriesOrder;
import com.ctshk.rpc.order.tour.entity.SeriesOrderTouristInfo;
import com.ctshk.rpc.order.tour.entity.SeriesOrderTouristQuit;
import com.ctshk.rpc.order.tour.entity.SeriesOrderTouristQuitNotice;
import com.ctshk.rpc.order.tour.mapper.SeriesOrderMapper;
import com.ctshk.rpc.order.tour.mapper.SeriesOrderTouristInfoMapper;
import com.ctshk.rpc.order.tour.mapper.SeriesOrderTouristQuitMapper;
import com.ctshk.rpc.order.tour.mapper.SeriesOrderTouristQuitNoticeMapper;
import com.ctshk.rpc.order.tour.req.SeriesOrderTouristQuitNoticeAddQuitItemReq;
import com.ctshk.rpc.order.tour.req.SeriesOrderTouristQuitNoticeAddReq;
import com.ctshk.rpc.order.tour.req.SeriesOrderTouristQuitNoticeDetailReq;
import com.ctshk.rpc.order.tour.service.ISeriesOrderService;
import com.ctshk.rpc.order.tour.service.ISeriesOrderTouristQuitNoticeService;
import com.ctshk.rpc.order.tour.util.Utils;
import com.ctshk.rpc.system.dto.UserDTO;
import com.ctshk.rpc.system.service.ISysUserService;
import com.ctshk.rpc.tour.dto.schedule.SeriesTripScheduleDTO;
import com.ctshk.rpc.tour.dto.schedule.SeriesTripScheduleDataDTO;
import com.ctshk.rpc.tour.req.CollectionPlaceJson;
import com.ctshk.rpc.tour.service.ISeriesTripScheduleService;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 退团通知书 服务实现类
 * </p>
 *
 * @author 谭响
 * @since 2021-02-04
 */
@DubboService
public class SeriesOrderTouristQuitNoticeServiceImpl extends ServiceImpl<SeriesOrderTouristQuitNoticeMapper, SeriesOrderTouristQuitNotice> implements ISeriesOrderTouristQuitNoticeService {

    @Autowired
    private SeriesOrderTouristQuitNoticeMapper seriesOrderTouristQuitNoticeMapper;
    @Autowired
    private SeriesOrderTouristQuitMapper seriesOrderTouristQuitMapper;
    @Autowired
    private SeriesOrderTouristInfoMapper seriesOrderTouristInfoMapper;
    @Autowired
    private SeriesOrderMapper seriesOrderMapper;

    @DubboReference
    private ISeriesTripScheduleService seriesTripScheduleService;
    @Autowired
    private ISeriesOrderService iSeriesOrderService;
    @DubboReference
    private ISysUserService iSysUserService;


    /**
     * 详情
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result<SeriesOrderTouristQuitNoticeDetailDTO> detail(SeriesOrderTouristQuitNoticeDetailReq req) {
        SeriesOrderTouristQuit dbQuit = seriesOrderTouristQuitMapper.selectById(req.getQuitId());
        if (dbQuit == null || Objects.equals(dbQuit.getIsDeleted(),1)){
            throw new BusinessException(SystemError.TOUR_ORDER_30022);
        }
        if (!Objects.equals(dbQuit.getRefundStatus(),3) && !Objects.equals(dbQuit.getRefundStatus(),5)){
            throw new BusinessException(SystemError.TOUR_ORDER_30023);
        }
        SeriesOrderTouristInfo touristInfo = seriesOrderTouristInfoMapper.selectById(dbQuit.getTouristId());
        SeriesOrder dbOrder = seriesOrderMapper.selectById(dbQuit.getOrderId());
        QueryWrapper<SeriesOrderTouristQuitNotice> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(SeriesOrderTouristQuitNotice::getQuitId, req.getQuitId());
        SeriesOrderTouristQuitNotice dbQuitNotice = seriesOrderTouristQuitNoticeMapper.selectOne(queryWrapper);
        SeriesOrderTouristQuitNoticeDetailDTO dto = null;
        SeriesTripScheduleDTO tripScheduleDTO = seriesTripScheduleService.getTripScheduleId(dbQuit.getScheduleId());
        SeriesTripScheduleDataDTO seriesTripScheduleDataDTO = tripScheduleDTO.getSeriesTripScheduleDataDTO();
        if (dbQuitNotice == null){
            //退团通知书
            List<SeriesOrderTouristQuitNoticeAddQuitItemReq> noticeAddQuitItemReqList = new ArrayList<>();
            SeriesOrderTouristQuitNoticeAddQuitItemReq noticeAddQuitItemReq = EntityUtil.copy(dbQuit, SeriesOrderTouristQuitNoticeAddQuitItemReq.class);
            noticeAddQuitItemReq.setCnSurname(touristInfo.getCnSurname());
            noticeAddQuitItemReq.setCnName(touristInfo.getCnName());
            noticeAddQuitItemReq.setEnSurname(touristInfo.getEnSurname());
            noticeAddQuitItemReq.setEnName(touristInfo.getEnName());
            noticeAddQuitItemReq.setGender(touristInfo.getGender());
            noticeAddQuitItemReq.setGroupFee(dbQuit.getDeductionGroupFee());
            noticeAddQuitItemReq.setTouristTypeName(touristInfo.getTouristTypeName());
            noticeAddQuitItemReq.setCollectionFee(dbQuit.getCollectionFee());
            noticeAddQuitItemReqList.add(noticeAddQuitItemReq);

            SeriesOrderTouristQuitNoticeAddReq noticeAddReq = new SeriesOrderTouristQuitNoticeAddReq();
            noticeAddReq.setCustomerRights(seriesTripScheduleDataDTO.getCustomerRights());
            noticeAddReq.setQuitReason(dbQuit.getQuitReason());
//        noticeAddReq.setCompanyNotes();
            noticeAddReq.setPassengerName(touristInfo.getEnSurname() + " " + touristInfo.getEnName() +
                    "(" + touristInfo.getCnSurname() + " " + touristInfo.getCnName() + ")");
            noticeAddReq.setTourCode(tripScheduleDTO.getTripNumber());
            noticeAddReq.setDays(seriesTripScheduleDataDTO.getTravelDays());
            noticeAddReq.setDepDate(Utils.getDayAndWeek(tripScheduleDTO.getDepartureDate().atStartOfDay()));
            noticeAddReq.setRoute(seriesTripScheduleDataDTO.getTitle());
            noticeAddReq.setEnquiryNo(Utils.getRandom6());
            noticeAddReq.setEnquiryTime(Constants.QUERY_TIME);
            noticeAddReq.setQuitItemList(noticeAddQuitItemReqList);
            noticeAddReq.setPaidInAmount(touristInfo.getTotalPaidIn());
            noticeAddReq.setRefundAmount(dbQuit.getRefundAmount());
            noticeAddReq.setGroupFee(touristInfo.getGroupFee());
            noticeAddReq.setOrderNumber(dbOrder.getOrderNumber());
            noticeAddReq.setReceiptNumber(iSeriesOrderService.getReceiptNumberQuit(req.getUserId()));
            noticeAddReq.setCollectionFee(dbQuit.getCollectionFee());
            UserDTO userInfo = iSysUserService.queryCacheUser(req.getUserId());
            if (userInfo != null) {
                if (StringUtils.isNotBlank(userInfo.getCnSurname()) && StringUtils.isNotBlank(userInfo.getCnName())) {
                    noticeAddReq.setCreateUser(userInfo.getCnSurname() + userInfo.getCnName());
                }
                noticeAddReq.setDepartmentName(userInfo.getDept());
                noticeAddReq.setAgent(userInfo.getDeptCode());
            }
            SeriesOrderTouristQuitNotice quitNotice = new SeriesOrderTouristQuitNotice();
            quitNotice.setOrderId(dbOrder.getId());
            quitNotice.setScheduleId(dbOrder.getScheduleId());
            quitNotice.setQuitId(dbQuit.getId());
            quitNotice.setTouristName(noticeAddReq.getPassengerName());
            quitNotice.setCustomerRights(noticeAddReq.getCustomerRights());
            quitNotice.setQuitReason(noticeAddReq.getQuitReason());
            quitNotice.setCompanyNotes(noticeAddReq.getCompanyNotes());
            quitNotice.setNotice(JSON.toJSONString(noticeAddReq));
            int insertQuitNotice = seriesOrderTouristQuitNoticeMapper.insert(quitNotice);
            if (insertQuitNotice <= 0){
                throw new BusinessException(SystemError.SYS_437);
            }
        }
        if (StringUtils.isNotBlank(dbQuitNotice.getNotice())) {
            dto = EntityUtil.copy(dbQuitNotice.getNotice(), SeriesOrderTouristQuitNoticeDetailDTO.class);
            if (dto != null) {
                dto.setOrderId(dbQuitNotice.getId());
                dto.setScheduleId(dbQuitNotice.getScheduleId());
                dto.setQuitId(dbQuitNotice.getId());
                dto.setPassengerName(dbQuitNotice.getTouristName());
                dto.setCustomerRights(dbQuitNotice.getCustomerRights());
                dto.setQuitReason(dbQuitNotice.getQuitReason());
                dto.setCompanyNotes(dbQuitNotice.getCompanyNotes());
                dto.setRemark(dbQuit.getRemark());
                dto.setQueryCode(CharUtil.getRandomStr(6));
                List<CollectionPlaceJson> collectionPlaceJson = tripScheduleDTO.getSeriesTripScheduleDataDTO().getCollectionPlaceJson();
                if (CollectionUtils.isNotEmpty(collectionPlaceJson)){
                    JSONObject placeJson = collectionPlaceJson.get(0);
                    dto.setQueryTime(placeJson.getString("date"));
                }
            }
        }
        return Result.success(dto);
    }
}
